#include "playerutility.h"
#include "playerutilityprivate.h"
#include "volumeKey.h"
#include <MdaAudioTonePlayer.h>
#include <QDebug>

PlayerUtilityPrivate* PlayerUtilityPrivate::NewL(const TDesC& aFileName ,PlayerUtility *wrapper)
{
    PlayerUtilityPrivate* self = NewLC(aFileName, wrapper);
    CleanupStack::Pop(self);
    return self;
}

PlayerUtilityPrivate* PlayerUtilityPrivate::NewLC(const TDesC& aFileName , PlayerUtility *wrapper)
{
    PlayerUtilityPrivate* self = new (ELeave) PlayerUtilityPrivate(wrapper);
    CleanupStack::PushL(self);
    self->ConstructL(aFileName);
    return self;
}

PlayerUtilityPrivate::~PlayerUtilityPrivate()
{
    if(m_playUtility)
    {
        m_playUtility->Stop();
        m_playUtility->Close();
    }

    delete m_playUtility;
}

PlayerUtilityPrivate::PlayerUtilityPrivate(PlayerUtility *wrapper):
    q_ptr(wrapper),
    m_isLoop(false),
    m_isImmediately(false)
{
}

void PlayerUtilityPrivate::ConstructL(const TDesC& aFileName)
{
    m_playUtility = CMdaAudioPlayerUtility::NewFilePlayerL(aFileName, *this);
    m_playing = m_prepared = EFalse;
}

void PlayerUtilityPrivate::play()
{
    if(m_prepared && !m_playing)
    {
        m_playUtility->Play();
        m_playing = ETrue;
    }
}

void PlayerUtilityPrivate::stop()
{
    m_playUtility->Stop();
    m_playing = EFalse;
}

void PlayerUtilityPrivate::pause()
{
    m_playUtility->Pause();
    m_playing = EFalse;
}

void PlayerUtilityPrivate::setLoops(bool loop)
{
    m_isLoop = loop;
}

void PlayerUtilityPrivate::MapcPlayComplete(TInt /*aError*/)
{
    m_playing = EFalse;
    if(m_isLoop)
    {
        m_playUtility->Play();
    }
}

void PlayerUtilityPrivate::MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& /*aDuration*/)
{
    if(aError == KErrNone)
    {
        m_prepared = ETrue;
        if(m_isImmediately)
        {
            m_playUtility->SetVolume(PlayerUtility::g_volume);
            m_playUtility->Play();
        }
    }
}


void PlayerUtilityPrivate::setVolume(int volume)
{
    m_playUtility->SetVolume(volume);
}

TInt PlayerUtilityPrivate::maxVolume()
{
    return m_playUtility->MaxVolume();
}

TInt PlayerUtilityPrivate::getVolume(TInt& aVolume)
{
    return m_playUtility->GetVolume(aVolume);
}

/******************************************************************************/
int PlayerUtility::g_count = 0;
int PlayerUtility::g_volume = 6500;
int PlayerUtility::g_maxVolume = 10000;
VolumeKey* PlayerUtility::g_volumeKey = NULL;

PlayerUtility::PlayerUtility(const QString& fileName, bool isRTSet) :    
    m_isRTSet(isRTSet)
{
    g_count++;
#ifdef Q_OS_SYMBIAN
TPtrC myDescriptor (reinterpret_cast<const TText*>(fileName.constData()),fileName.length());
d_ptr = PlayerUtilityPrivate::NewL(myDescriptor, this);
if(g_volumeKey == NULL)
{
    g_volumeKey = new VolumeKey(this);
}
connect(g_volumeKey,SIGNAL(sig_volumeKeyPressed(int)),SLOT(volumeKeyPressed(int)));

#endif

}

PlayerUtility::~PlayerUtility()
{
//    qWarning("~PlayerUtility");
    g_count--;
    delete d_ptr;
}

void PlayerUtility::play()
{   
    //    qWarning()<<"play";
    this->setVolume();
    d_ptr->play();
}

void PlayerUtility::stop()
{
    d_ptr->stop();
}

void PlayerUtility::volumeKeyPressed(int upOrDown)
{
    static int mask = 0;
    mask++;
    qWarning()<<"mask gcount"<<mask<<g_count;
    if(mask%g_count == 1)
    {
        if(upOrDown == EVolumeDown)
        {
            g_volume -= g_maxVolume>>4;
        }
        else
        {
            g_volume += g_maxVolume>>4;
        }    
        g_volume = qBound(0,g_volume,g_maxVolume);
    }
    if(m_isRTSet)
        this->setVolume();
}

void PlayerUtility::setLoops(bool loop)
{
    d_ptr->setLoops(loop);
}

void PlayerUtility::setVolume()
{
//    qWarning()<<"volume"<<g_volume;
    d_ptr->setVolume(g_volume);
}

void PlayerUtility::setImmediately(bool immediately)
{
    d_ptr->setImmediately(immediately);
}

bool PlayerUtility::IsPlaying()
{
    return d_ptr->IsPlaying();
}
